URL = "ftp://xmlsoft.org/libxml2/libxslt-%(VERSION)s.tar.gz"
VERSIONS = ["1.1.26"]
DEFAULT_VERSION = "1.1.26"
DEPENDS = ["zlib", "libxml2"]

import os, shutil, subprocess
from source.builders import NMakeBuilder, ConfigureMakeBuilder
from source.machine import Collector

#todo linux
# libxml2 not found
# systems python found

class LibXSLTBuilder( NMakeBuilder ):
  """ subclass of NMakeBuilder with custom configure step """
 
  def create3rdParty( self ):
    """get the directories of the 3rd party libs from the other modules into one dir"""    
    thirdPartyDir = self.directories.build+"\\libxml2ext"
    Collector([ self.dependencies["zlib"].targetpath(), self.dependencies["libxml2"].targetpath()], thirdPartyDir, removeandcreatedir = True)
    
  def configure( self ):
    self.create3rdParty()
    os.chdir( self.extractedpath+"\\win32" )

    command = "cscript configure.js compiler=msvc "
    
    installpath = self.installpath()
    if self.debug:
      installpath += "\\debug"
      command += " debug=yes"      
    else:
      command += " debug=no"
      
    command += " prefix="+installpath
    command += " sodir="+installpath+"\\bin " 
    command += " include="+self.directories.build+"\\libxml2ext\\include"
    command += " lib="+self.directories.build+"\\libxml2ext\\lib"
    command += " zlib=yes"
    command += " iconv=no"
    command += " vcmanifest=yes"
      
    print( "executing: "+command )
    self.execute(command)
    
    
  def makefile(self):
    return "Makefile.msvc"

  def make(self ):
    os.chdir( self.extractedpath+"\\win32" )
    self.execute( "nmake /f " + self.makefile() )

  def install(self):
    self.execute( "nmake install " )

class LibXSLTConfigureMakeBuilder( ConfigureMakeBuilder ):
  """ subclass of NMakeBuilder with custom configure step """
  def configure( self ):
    os.environ['PATH'] =  os.pathsep.join([ self.dependencies["libxml2"].targetpath()+os.sep+"bin", os.environ['PATH'] ])
    ConfigureMakeBuilder.configure(self)
 
def decider( machine, dirs ):
  """ build slt for the machine in dirs """
  return (LibXSLTConfigureMakeBuilder, LibXSLTBuilder)[machine.isWindows()]
 
BUILDER = decider
